<?php

namespace app\index\controller;


use app\lib\enum\Status;
use think\Db;

class Institution extends BaseController
{
    //培优列表
    public function institutionList(){

        //查找培训机构信息
        if(request()->isAjax()){
            $page=input("post.page");
            $limit=config("setting.institutionNumPerPage");
            //搜索条件
            //机构名称
            $ins_name=input("post.ins_name");
            //机构所包含的课程
            $ins_lessons=input("post.ins_lessons");
            $this->assign("ins_name",$ins_name);
            $this->assign("ins_lessons",$ins_lessons);



            $condition=[];
            $condition['ins_status']=['eq',Status::on];
            $latitude=input("post.latitude");
            $longitude=input("post.longitude");
            $latitude=$latitude==-1?"30.2744123119":$latitude;
            $longitude=$longitude==-1?"120.4731988907":$longitude;

            $sql='select *,st_distance_sphere(point(%s,%s),point(ins_longitude,ins_latitude)) as juli from py_institution where %s  %s %s';
            $whereCondition=" ins_status = ".Status::on;
            //筛选指定城市下的机构信息
            $cityId=session("cityId");
            $whereCondition.=" and ins_city_id = ".$cityId." ";

            //处理筛选条件
            if($ins_name!=-1){
                $whereCondition.=" and ins_name like '%".$ins_name."%'";
            }
            if($ins_lessons!=-1){
                $whereCondition.=" and ins_lessons like '%".$ins_lessons."%' ";
            }

            //处理排序条件
            //排序方式
            $orderType=input("post.orderType");
            $order="";
            //距离排序
            if($orderType=="hot"){
                $order=" order by ins_istop asc ,ins_view_times desc ";
            }else{
                $order=" order by ins_istop asc ,juli asc ";
            }
            $limitStr="limit ".($page-1)*$limit.",".$limit;
            $sql=sprintf($sql,$longitude,$latitude,$whereCondition,$order,$limitStr);
            $institutionList=Db::query($sql);

            $hasMore=count($institutionList)==$limit?Status::on:Status::off;
            $html="";
            foreach ($institutionList as $value){
                $tempStr=' <li>
                <div class="img">
                    <a href="%s"  ><img src="%s"></a>
                </div>
                <a href="%s"  >
                    <div class="text">
                        <p>%s %s</p>
                        <p class="ptime">%s <span>距离%sKM</span></p>
                        <p class="ptime">浏览次数：%s</p>
                    </div>
                </a>
            </li>';
//                $url=ture?"/index/Institution/institutionDetail?ins_id=".$value['ins_id']."&location=".$longitude.",".$latitude:$value['ins_url'];
                $url="/index/Institution/institutionDetail?ins_id=".$value['ins_id']."&location=".$longitude.",".$latitude;
                $topHtml=$value['ins_istop']==Status::on?"<span class=\"topPosition\">置顶</span>":"";
                $value['juli']=round(($value['juli']/1000),2);
                $value['ins_thumb']=$value['ins_thumb']?$value['ins_thumb']:cache("webInfo")['web_institution_default_image'];
                $tempHtml=sprintf($tempStr,$url,$value['ins_thumb'],$url,$value['ins_name'],$topHtml,$value['ins_location'],$value['juli'],$value['ins_view_times']);
                $html.=$tempHtml;
            }
            $data=[
                "html"=>$html,
                "hasMore"=>$hasMore
            ];

            $this->success("","",$data);
        }

        if(!session("cityId")){
            //如果没有选择城市，就跳转到首页
            $this->redirect("index/index/index");
        }

        //筛选信息
        $ins_name=input("get.ins_name");
        $ins_lessons=input("get.ins_lessons");
        $ins_name=$ins_name==null?-1:$ins_name;
        $ins_lessons=$ins_lessons==null?-1:$ins_lessons;

        $this->assign("ins_name",$ins_name);
        $this->assign("ins_lessons",$ins_lessons);

        //排序信息
        $orderType=input("get.order");
        $orderType==""?"distance":$orderType;
        $this->assign("orderType",$orderType);

        //返回SDK参数
        $this->assign("SDKParams",getSDKParams());
        return view();
    }

    //培优详情
    public function institutionDetail(){
        $ins_id=input("get.ins_id");
        $location=input("get.location");
        if($location!=""){
            $longitude=explode(",",$location)[0];
            $latitude=explode(",",$location)[1];
        }
        $institutionInfo=model("PyInstitution")->with("bindCity")->find($ins_id);
        //浏览次数+1
        institutionViewTimesAdd($institutionInfo);

        $sql="select st_distance_sphere(point(%s,%s),point(ins_longitude,ins_latitude)) as juli from py_institution where ins_id = ".$ins_id;

        $sql=sprintf($sql,$longitude,$latitude);
        $info=Db::query($sql);
        $juli=round(($info[0]['juli'])/1000,2);
        $this->assign("juli",$juli);
        //获得用于分享的url地址，即本页面地址
        $institutionInfo["url"]=getUrl();
        $institutionInfo['imageUrl']="http://".$_SERVER['SERVER_NAME'].str_replace("\\","/",$institutionInfo['ins_thumb']);
        $institutionInfo['desc']=getStringFromRichText($institutionInfo['ins_content']);
        $this->assign("institutionInfo",$institutionInfo);

        $this->assign("longitude",$institutionInfo['ins_longitude']);
        $this->assign("latitude",$institutionInfo['ins_latitude']);
        //获取轮播图参数
        $thumbList=[];
        if($institutionInfo['ins_thumb']!=""){
            $thumbList[]=$institutionInfo['ins_thumb'];
        }
        if($institutionInfo['ins_thumb2']!=""){
            $thumbList[]=$institutionInfo['ins_thumb2'];
        }
        if($institutionInfo['ins_thumb3']!=""){
            $thumbList[]=$institutionInfo['ins_thumb3'];
        }
        $this->assign("thumbList",$thumbList);

        //获取SDK参数
        $this->assign("SDKParams",getSDKParams());

        //判断该机构是填写了跳转地址ins_url
        //如果填写了地址，则不显示机构的ins_content，而是显示跳转按钮
        //如果未填写跳转地址，就正常显示机构的ins_content
        $hasInsUrl=true;
        if($institutionInfo['ins_url']===""||$institutionInfo['ins_url']===null){
            $hasInsUrl=false;
        }
        $this->assign("hasInsUrl",$hasInsUrl);
        return view();
    }

    //搜索条件
    public function institutionSearch(){
        //查找培训机构的授课列表
        $lessons=model("CmsArticleCategory")->where("acc_id","in",getGoodClassSubCategoryIds())->select();
        $this->assign("lessons",$lessons);
        return view();
    }


}
